/************************************************/
/************************************************/
/**** PART 2: GENERATE VARIABLES ****/
/************************************************/
/************************************************/

** A) grade dummy

	tab grade1, gen(gr)
	
	
** B) Teacher reported bad behavior category

	replace fphysicalbully=. if btname_gone==1
	replace fverbalbully=. if btname_gone==1
	replace ftemper=. if btname_gone==1
	

/**************************************************/
*** District, School and Province Level Variable ***
/**************************************************/

	tab district, gen(dist)
	tab b_schoolid, gen(sch)

** Inter-ethnic Nominations 
	
	** number of host nominations
	
	ge bfriend_total_host=bnode_out_friend-bfriend_total_refugee
	ge bsupport_total_host=bnode_out_support-bsupport_total_refugee
	ge bstudy_total_host=bnode_out_study-bstudy_total_refugee
	ge bsupport_self_total_host=bnode_out_supportself-bsupport_self_total_refugee
	ge bstudy_self_total_host=bnode_out_studyself-bstudy_self_total_refugee


** teacher determines the seating

	destring btsurvey_22_2, replace force
	ge teacherseat=0 if btsurvey_22_2==2
	replace teacherseat=1 if btsurvey_22_2==1
	
	
/************************************************/
*** Student Level Variables ***
/************************************************/	

	
** bullying reported by student - aggregate
		
	egen bsbully=rowmean(bsurvey01_book3 bsurvey02_book3 bsurvey03_book3 bsurvey04_book3 bsurvey05_book3 bsurvey06_book3)
	replace bsbully=bsbully*6
	replace bsbully=. if  bsurvey01_book3==. & bsurvey02_book3==. & bsurvey03_book3==. & bsurvey04_book3==. & bsurvey05_book3==. & bsurvey06_book3==.
	gen d_bsbully=0 if bsbully==0
	replace d_bsbully=1 if bsbully!=0 & !missing(bsbully)

** number of terms in the classroom  for refugee students
	
	gen ref_s_exposure=s_exposure if refugee==1
	replace ref_s_exposure=. if refugee==0
			
** student exposure (number of terms) to refugees in the school

	gen ref_bsterm=bsterm if refugee==1 & data_baseline=="1"

	by b_schoolid, sort: egen m_refexposure=mean(ref_bsterm)
	replace m_refexposure=. if data_baseline~="1"

	gen s_ref_exposure=m_refexposure if bsterm>m_refexposure
	replace s_ref_exposure=bsterm if m_refexposure>=bsterm

	replace s_ref_exposure=2 if s_ref_exposure>1 & s_ref_exposure<2
	replace s_ref_exposure=3 if s_ref_exposure>2 & s_ref_exposure<3
	replace s_ref_exposure=4 if s_ref_exposure>3 & s_ref_exposure<4
	replace s_ref_exposure=5 if s_ref_exposure>4 & s_ref_exposure<6	

	replace s_ref_exposure=5 if s_ref_exposure==6
	
** ethnic bias followup

	replace fethnicbias=. if btname_gone==1	

** STANDARDIZATION POOLED  & HOST MEAN 0

	destring braven, force replace
		foreach i in braven beyes_correct math_correct turk_correct  fphysicalbully fverbalbully ftemper fethnicbias { 
			sum `i' if refugee==0 & sample1==1
			gen `i'_sd=.
			replace `i'_sd=(`i'-r(mean))/r(sd)
		}
		
		capture drop bmath_std bturk_std
		rename beyes_correct_sd beyes_sd
		rename math_correct_sd bmath_std
		rename turk_correct_sd bturk_std

/************************************************/
*** Teacher and Class Level Variables ***
/************************************************/


** tenured baseline_t

	ge ttenured=1 if bttenured=="TENURED"
	replace ttenured=0 if bttenured!="TENURED" & bttenured!="EMPTY"
	 
** education

	ge teduc=1 if bteducation=="3"
	replace teduc=2 if bteducation=="2"
	replace teduc=3 if bteducation=="4"
	label define teduc 1 "4 year uni" 2 "2 year college" 3 "grad school" 
	label values teduc teduc	

** marital Status

	ge tmarried=1 if btmarital=="MARRIED"
	replace tmarried=0 if btmarital=="SINGLE"

** eyes(cognitive empathy)

	preserve
	collapse bteyes sample1, by(btname)
	sum bteyes if sample1!=0
	local mean1 = r(mean)
	local sd1 = r(sd)
	restore
	gen bteyes_sd=(bteyes-`mean1')/`sd1'


** teacher raven

	destring btraven,replace force
	preserve
	collapse btraven sample1, by(btname)
	sum btraven if sample1!=0
	local mean1 = r(mean)
	local sd1 = r(sd)
	restore
	gen btraven_sd=(btraven-`mean1')/`sd1'


** teaching styles

	foreach var of varlist b_pedagogy b_gms b_idealism b_competence b_modern b_extrinsic b_warmth{
	preserve
	collapse `var' sample1, by(btname)
	sum `var' if sample1!=0
	local mean1 = r(mean)
	local sd1 = r(sd)
	restore
	gen `var'_sd=(`var'-`mean1')/`sd1'
	}


** ethnic bias IAT standardization

	preserve
	collapse biat_ethnic sample1, by(btname)
	sum biat_ethnic if sample1!=0
	local mean1 = r(mean)
	local sd1 = r(sd)
	restore
	gen biat_ethnic1=(biat_ethnic-`mean1')/`sd1'

	rename biat_ethnic raw_biat
	rename biat_ethnic1 biat_ethnic

	
** teacher level refugee exposure	

	ge rexposure=0 if refshare==0 & sample0==1
	replace rexposure=1 if baseline_tterm==1 & sample1==1
	replace rexposure=2 if (baseline_tterm==2) & sample1==1
	replace rexposure=3 if (baseline_tterm>2 &  baseline_tterm~=.) & sample1==1

	bysort b_classid: egen ref_exposure= max(ref_s_exposure) if sample1==1
	replace ref_exposure=0 if refshare==0 & sample0==1


